package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.Setmeal;
import com.example.domain.SetmealDish;
import com.example.dto.SetmealDto;
import com.example.service.CategoryService;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;




    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {


        Page<Setmeal> page1 = new Page(page, pageSize);

        Page<SetmealDto> page2 = new Page();

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<Setmeal>();

        wrapper.orderByDesc(Setmeal::getUpdateTime);

        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        setmealService.page(page1, wrapper);

        BeanUtils.copyProperties(page1,page2,"records");

        List<Setmeal> records = page1.getRecords();

        List<SetmealDto> setmealDtos = records.stream().map(setmeal -> {

            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(setmeal, setmealDto);

            Category byId = categoryService.getById(setmeal.getCategoryId());

            if(byId!=null) setmealDto.setCategoryName(byId.getName());

            return setmealDto;

        }).collect(Collectors.toList());

        page2.setRecords(setmealDtos);

        return R.success(page2);
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

         setmealService.saveWithDishes(setmealDto);

        return R.success("添加成功！");
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping
    public R<String> changeSaves(@RequestBody SetmealDto setmealDto){

        setmealService.removeById(setmealDto.getId());
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmealDto.getId()));
        setmealService.saveWithDishes(setmealDto);

        return R.success("添加成功！");
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @GetMapping("/{id}")
    public R<Setmeal> update(@PathVariable Long id){

        Setmeal byId = setmealService.getWithDish(id);

        return R.success(byId);
    }
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.removeWithDishes(ids);

        return R.success("删除成功！");

    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> change(@PathVariable Integer status, @RequestParam List<Long> ids){

        setmealService.changeStatus(status, ids);

        return R.success("修改成功！");
    }

    @Cacheable(value = "setmeal", key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<Setmeal>();

        wrapper.eq(setmeal.getCategoryId()!=null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(wrapper);

        return R.success(list);
    }




}
